package com.hc.componentspringboot.repository;


import com.hc.componentspringboot.bean.Discuss;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Spring Data JPA 接口实现  半自动化映射
 * 1.导入依赖
 * 2.表映射实体类
 * 3.实现JpaRepository
 */
public interface DiscussRepository extends JpaRepository<Discuss,Integer> {
    List<Discuss> findByAuthorNotNull();
    List<Discuss> findByAuthorNotNullOrderByIdDesc();
    List<Discuss> findByArticleIdAndAuthor(Integer articleId,String author);
    @Query("select c from t_comment c where c.articleId = ?1")
    List<Discuss> getDiscussPaged(Integer articleId, Pageable pageable);

    @Query(value = "select * from t_comment where article_id = ?1",nativeQuery = true)
    List<Discuss> getDiscussPaged2(Integer articleId, Pageable pageable);

    @Modifying //非select语句需要
    @Transactional //非select语句需要
    @Query(value = "update t_comment set author =?2 where id =?1",nativeQuery = true)
    Integer updateDiscuss(Integer id,String author);

    @Modifying
    @Transactional
    @Query(value = "delete from t_comment where id = ?1",nativeQuery = true)
    Integer deleteDiscuss(Integer id);
}
